#! /usr/local/bin/perl
sub generate_random_password
{
	my $passwordsize = shift;
	my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
	my $randpassword = join '',
	map $alphanumeric[rand @alphanumeric], 0..$passwordsize;

	return $randpassword;
}
my ($sec,$min,$hour,$mday,$mon,$year,
	$wday,$yday,$isdst) = localtime time;
	$year += 1900;
	$mon += 1;
	$date = $year."-".$mon."-".$mday;
	use DBI;
	$user = "vbcll";
	$password = "v0lle7";
	$db = "vbcll_ch_-_vbcll2";
	$pid = $$;
	$dsn = "DBI:mysql:database=$db;host=localhost";
	$dbh = DBI->connect($dsn,$user,$password);
	$r = "select distinct(id_vbcll) , url_classement from equipes inner join matchs on (matchs.equipe = equipes.nom) where resultat not like '%-%' union (select distinct(id_vbcll) , url_classement from equipes inner join matchs on (matchs.equipe = equipes.nom and matchs.resultat like '%-%' and date_add(matchs.date,interval 7 day) > curdate()) where id_vbcll not in (select distinct(id_vbcll) from equipes inner join matchs on (matchs.equipe = equipes.nom and matchs.resultat not like '%-%')))";
	$s = $dbh->prepare($r);
	$s->execute;
	while(@out = $s->fetchrow_array()){
		# print $out[0].":".$out[1]."\n";
		$id = $out[0];
		$url = $out[1];
		$resultats = "";
		$classement = "";
		if($url){
			@lines = [];
			@rows = [];
			$all = '';
			$line = '';
			$row = '';
			$rank = '';
			$file = "/var/www/html/classements/".$id.".html";
			system("curl -s -o ".$file." '".$url."'");
			if(!open(FILE,$file)){ print "Unable to open file $file from url: $url\n";}
			else{
				@lines = <FILE>;
				close(FILE);
				#        unlink($file);
				foreach $line (@lines){
					chop($line);
					$line =~s/[\r\n]//g;
					#       $line =~ s/(<t[dr] .*?>)/<t$1>/g;
					#            $line =~ s/<font[\ a-zA-Z0-9"=]*>//g;
					#            $line =~ s/<\/font>//g;
					#            $line =~ s/<\/img>//g;
					#            $line =~ s/<img.*?>//g;
					$line =~ s/<[^t\/].*?>//g;
					$line =~ s/<\/[^t].*?>//g;
					$all .= $line;
				}
				######### InfoSports ###########
				if($url){
					if($url =~ /svrv/){
						$all =~ /(<table.*?<\/table>)(.*?)(<table.*?<\/table>)(.*?)(<table.*?<\/table>)/;
						$classement = $3;
						$resultats = $5;
					}
					else{
						$all =~ /(<table.*?<\/table>)(.*?)(<table.*?<\/table>)/;
						$classement = $3;
						$resultats = $1;
					}
					$row = "<p class = 'bold'>R&eacute;sultats</p>".$resultats."<p class = 'bold'>Classement</p>".$classement;
					open(OUT,">/var/www/html/classements/".$id.".html");
					$row =~ s/<table .*?>/<table>/g;
					$row =~ s/<t([rd]).*?>/<t$1>/g;
					$row =~ s/>\s*</></g;
					$classement =~ s/<table .*?>/<table>/g;
					$classement =~ s/<t([rd]).*?>/<t$1>/g;
					$classement =~ s/>\s*</></g;

					print OUT $row;
					close(OUT);
					@rows = split(/<tr>/,$classement);
					$class = 0;
					$rank = 1;
					for($i = 0; $i <= $#rows; $i++){
						$ok=0;
						@cols = split(/<td>/,$rows[$i]);
						$class = $cols[1];
						$class =~ s/&#160;//g;
						$class =~ s/[^0-9]//g;
						if($class){ $rank = $class;}
						if($id eq 'MJA2' && $rows[$i] =~/LUTRY-LAVAUX II/){ $ok=2;}
						elsif($id eq 'MJA1' && $rows[$i] =~/LUTRY-LAVAUX/){ $ok=1;}
						#                elsif($id eq 'MJA1' && $rows[$i] =~/LUTRY-LAVAUX I[^I]/){ $ok=1;}
						elsif( ($id ne "MJA1" && $id ne "MJA2" && $rows[$i] =~/(L[Uu][tT][rR][yY])/)){$ok=3;}
						if($ok){
							#                    print $cols[1]." => ".$rank;
							if($url =~ /infosport/){ $nb_total = $#rows -2;}
							else{ $nb_total = $#rows-1;}
							#            print $id."\t".$rank."/".$nb_total."\n";
							$rank = $rank."/".$nb_total;
							$q = "select id from equipes where id_vbcll = '$id'";
							$r = $dbh->prepare($q);
							$r->execute;
							$id = $r->fetchrow_array();
							$req = "insert into classements values ('','$id','$rank','$date')";
							$get_result = $dbh->prepare($req);
							$get_result->execute;
						}
					}

				}
			}
		}
	}

